package cn.itsource.ymcc.service.impl;

import cn.itsource.ymcc.client.LogininfoClient;
import cn.itsource.ymcc.domain.Employee;
import cn.itsource.ymcc.domain.Logininfo;
import cn.itsource.ymcc.mapper.EmployeeMapper;
import cn.itsource.ymcc.result.JsonResult;
import cn.itsource.ymcc.service.IEmployeeService;
import cn.itsource.ymcc.util.AssertUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whale.chen
 * @since 2023-02-23
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Autowired
    private LogininfoClient logininfoClient;

    @GlobalTransactional
    @Override
    public boolean save(Employee entity) {
        //1.openfeign调用认证中心接口，保存登录账号信息
        Logininfo logininfo = new Logininfo()
                .setUsername(entity.getRealName())
                .setPassword(new BCryptPasswordEncoder().encode("123"))
                .setType(0);
        JsonResult jsonResult = logininfoClient.saveOrUpdate(logininfo);
        AssertUtil.isTrue(jsonResult.getSuccess(), jsonResult.getMessage());
        //使用openfeign调用三方接口：三方接口返回的值是Long，但经过openfeign后，变成了Integer
        Long logininfoid = ((Integer) jsonResult.getData()).longValue();


        entity.setLoginId(logininfoid);
        return super.save(entity);
    }
}
